package com.lilei.bigdata.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.net.URI;

public class HdfsClientDemo {
    FileSystem fs = null;
    Configuration conf = null;

    @Before
    public void init() throws Exception{
        conf = new Configuration();
        conf.set("dfs.replication", "5");

        fs =FileSystem.get(new URI("hdfs://mini1:9000"),conf,"root");
    }

    //上传文件
    @Test
    public void testUpload() throws Exception{
        fs.copyFromLocalFile(new Path("d://hehe.txt"),new Path("/"));
        fs.close();
    }

    //下载文件
    @Test
    public void testDownload() throws Exception{
        fs.copyToLocalFile(new Path("/hehe.txt"),new Path("g:/"));
        fs.close();
    }

    //创建删除目录
    @Test
    public void testMkdirAndDeleteAndRename() throws Exception{
        //创建文件夹
        fs.mkdirs(new Path("/a1/b1/c1"));
        //删除目录
        fs.delete(new Path("/aa"),true);
        //重命名
        fs.rename(new Path("/a1"),new Path("/a11111"));
    }

    //显示目录信息,只显示文件
    @Test
    public void testListFiles() throws Exception{
        RemoteIterator<LocatedFileStatus> listFiles =   fs.listFiles(new Path("/"),true);
        while (listFiles.hasNext()){
            LocatedFileStatus fileStatus = listFiles.next();
            System.out.println(fileStatus.getPath().getName());
            System.out.println(fileStatus.getBlockSize());
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getLen());
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            for (BlockLocation bl : blockLocations) {
                System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());
                String[] hosts = bl.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }
            }
            System.out.println("----------------------------");
        }
    }

    /**
     * 查看文件及文件夹信息
     *
     * @throws Exception
     */
    @Test
    public void testListAll() throws Exception {

        FileStatus[] listStatus = fs.listStatus(new Path("/"));

        String flag = "d--             ";
        for (FileStatus fstatus : listStatus) {
            if (fstatus.isFile()){
                flag = "f--         ";
            } else {
                flag = "d--             ";
            }

            System.out.println(flag + fstatus.getPath().getName());
        }
    }
}
